<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'
import {mRequest} from '@/utils/request'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      dataFormConfig: {
        id: {
          hidden: true
        },
        ingredientInfoId: {
          label: '辅料',
          type: 'list',
          placeholder: '请选择辅料',
          otherInfo: {label: '供应商：', attrName: 'supplierName'},
          list: [],
          isNeedAdd: true,
          addNewItemFunc: this.addNewIngredient,
          selectCallback: this.ingredientIdSelectCallback
        },
        supplierId: {
          label: '供应商',
          type: 'list',
          placeholder: '请选择供应商',
          list: []
        },
        weight: {
          label: '吨数',
          type: 'number',
          max: 1000,
          default: 0
        },
        price: {
          label: '价格',
          type: 'number',
          max: 1000000,
          default: 0
        },
        tickerNumber: {
          label: '票号',
          placeholder: '请输入票号'
        },
        dayTime: {
          label: '日期',
          type: 'date',
          placeholder: '请选择日期'
        },
        plateNumber: {
          label: '车牌号',
          placeholder: '请输入车牌'
        },
        freight: {
          label: '运费',
          type: 'number',
          max: 1000,
          default: 0
        },
        imageUrl: {
          label: '图片',
          type: 'uploadImage',
          imageType: 'uploadSupplierImage'
        },
        sourceType:{
          label: '来源方式',
          type: 'radio',
          radioBorder: true,
          radioList: [{text: '三和', label: 0}, {text: '外协', label: 1}],
          default: 1
        },
        isPay: {
          label: '付款状态',
          type: 'radio',
          radioBorder: true,
          radioList: [{text: '未付款', label: 0}, {text: '已付款', label: 1}],
          default: 0
        },
        status: {
          label: '状态',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 1
        }
      }
    }
  },
  methods: {
    addNewIngredient() {
      const cb = (newValue) => {
        this.initIngredientInfoList()
        this.dataForm.ingredientInfoId = newValue.id
      }
      this.$parent.openIngredientInfoDialog(cb)
    },
    ingredientIdSelectCallback() {
      //getSupplierListByIngredientId

      this.dataForm.supplierId = ''

      mRequest('supplierApi/getSupplierListByIngredientId', {id: this.dataForm.ingredientInfoId}, ({data}) => {
        this.dataFormConfig['supplierId'].list = this.R.propOr([], 'list', data)
      })

    },
    init(id) {
      this.dataForm.id = id || 0
      this.initIngredientInfoList()
      this.$refs.dialog.initDialog({
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        dataRule: this.getNeedDataRule('ingredientInfoId', 'weight', 'dayTime', 'isPay', 'status'),
        initApi: 'ingredientApi/ingredientAddInfo',
        saveApi: 'ingredientApi/saveIngredientAdd',
        updateApi: 'ingredientApi/updateIngredientAdd',
        objectName: 'ingredientAdd'
      })
    }
  }
}
</script>
